package com.panda.pandawash.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.panda.pandawash.R;
import com.panda.pandawash.api.ApplicationConstant;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.bean.FeedbackBean;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.util.HttpUtil;
import com.panda.pandawash.view.StatusBarCompat;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by gaowf on 2017/2/21.
 */

public class FeedbackActivity extends Activity implements View.OnClickListener {

    private final static String TAG = "FeedbackActivity";
    private SharedPreferences mSp;
    private SharedPreferences.Editor mEditor;
    private Button mFeedbackBt;
    private EditText mFeedbackTitleEt;
    private EditText mFeedbackContentEt;
    private ImageView mFeedback;

    private String getCommitDate;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feedback_activity);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            StatusBarCompat.compat(this, getResources().getColor(R.color.colorPrimary));
        }
        mFeedbackBt = (Button) findViewById(R.id.feedback_submit_bt);
        mFeedbackTitleEt = (EditText) findViewById(R.id.feedback_title_et);
        mFeedbackContentEt = (EditText) findViewById(R.id.feedback_content_et);
        mFeedback = (ImageView)  findViewById(R.id.feedback_activity_return);
        mFeedbackBt.setOnClickListener(this);
        mFeedback.setOnClickListener(this);
        mSp = getSharedPreferences(ApplicationConstant.SP_NAME, MODE_PRIVATE);
        mEditor = mSp.edit();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.feedback_activity_return){
            finish();
        }else if(v.getId()==R.id.feedback_submit_bt){
            promoteSend();
            /*String title = mFeedbackTitleEt.getText().toString().trim();
            String content = mFeedbackContentEt.getText().toString().trim();
            if (!"".equals(title) && !"".equals(content)) {
                String date_content = getFeedbackDate() + content;
                postFeedbackInfo(title,date_content);
                finish();

            }*/
        }
    }

    private void promoteSend(){
            String account = CDApplication.getIntance().getUserInfo().getUseraccount();
            String title = mFeedbackTitleEt.getText().toString().trim();
            String content = mFeedbackContentEt.getText().toString().trim();
            String date = getFeedbackDate();
            if(!"".equals(title) && !"".equals(content)){
                FeedbackBean mFeedbackBean = new FeedbackBean();
                mFeedbackBean.setTitle(title);
                mFeedbackBean.setContent(content);
                mFeedbackBean.setDate(date);
                HttpUtil.feedbackSubmit(new FeedbackSendCallback(),account,account,mFeedbackBean);

            }else{
                showMessage(getResources().getString(R.string.feedback_content_not_null));

            }

    }

    private class FeedbackSendCallback extends AsyncHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, String content) {
            super.onSuccess(statusCode, content);
            if (statusCode == 200) {
                HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, null);
                Log.d(TAG, "resultBean = " + resultBean);
                if (resultBean != null) {
                    String code = resultBean.getCode();
                    String message = resultBean.getMessage();
                    Log.d(TAG, "code = " + code);
                    if (code.equals("0")) {
                        showMessage(getResources().getString(R.string.feedback_send_success));
                        finish();
                    } else if (code.equals("99")) {
                        showMessage(message);
                    }
                }
            } else {
                showMessage(getResources().getString(R.string.feedback_send_fail));
            }
        }

        @Override
        public void onFailure(Throwable error, String content) {
            super.onFailure(error, content);

            Log.d(TAG, "onFailure = " + error);
            if (error.toString().contains("refused")) {

            }
        }
    }

    private void postFeedbackInfo(String title,String content){
        mEditor.putString(title, content);
        mEditor.commit();
    }

    //字节18
    private String getFeedbackDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        getCommitDate = df.format(new Date());// new Date()为获取当前系统时间
        return getCommitDate;
    }



    /**
     *
     * @param string
     * 需要显示的字串提示
     */

    private void showMessage(String string) {
        Toast toast = Toast.makeText(FeedbackActivity.this, string, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}
